<?php

/**
 * This is the model class for table "item".
 *
 * The followings are the available columns in table 'item':
 * @property integer $item_id
 * @property string $product_id
 * @property string $supplier_id
 * @property string $title
 * @property string $description
 * @property double $price
 * @property string $quantity
 * @property integer $sales
 * @property string $features
 * @property string $size
 * @property string $color
 * @property string $material
 * @property string $itemscol
 * @property integer $is_onstock
 *
 * The followings are the available model relations:
 * @property Inventory[] $inventories
 * @property Product $product
 */
class Item extends CActiveRecord
{

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Item the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'item';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('sales, is_onstock', 'numerical', 'integerOnly' => true),
            array('price', 'numerical'),
            array('product_id, supplier_id, quantity', 'length', 'max' => 10),
            array('title, material', 'length', 'max' => 45),
            array('description', 'length', 'max' => 150),
            array('code','length', 'max' => 32),
            array('features', 'length', 'max' => 200),
            array('size', 'length', 'max' => 20),
            array('color', 'length', 'max' => 40),
            array('code', 'required', 'message'=>'Mã sản phẩm không được để trống'),
            array('code', 'unique', 'message' => 'Mã sản phẩm này đã tồn tại'),
            array('product_id', 'required', 'message'=>'Vui lòng chọn một sản phẩm'),
            array('supplier_id', 'required', 'message'=>'Vui lòng chọn một nhà cung cấp'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('item_id, product_id, supplier_id, code, title, description, price, quantity, sales, features, size, color, material, is_onstock', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'inventories' => array(self::HAS_MANY, 'Inventory', 'item_id'),
            'product' => array(self::BELONGS_TO, 'Product', 'product_id'),
            'supplier' => array(self::BELONGS_TO, 'Supplier', 'supplier_id')
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'item_id' => ProductModule::t('Id','item'),
            'product_id' => ProductModule::t('Product','item'),
            'supplier_id' => ProductModule::t('Supplier','item'),
            'title' => ProductModule::t('Title','item'),
            'code'  => 'Mã sản phẩm',
            'description' => ProductModule::t('Description','item'),
            'price' => ProductModule::t('Price','item'),
            'quantity' => ProductModule::t('Quantity','item'),
            'sales' => ProductModule::t('Sales','item'),
            'features' => ProductModule::t('Features','item'),
            'size' => ProductModule::t('Size','item'),
            'color' => ProductModule::t('Color','item'),
            'material' => ProductModule::t('Material','item'),
            'is_onstock' => ProductModule::t('Is Onstock','item'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('item_id', $this->item_id, true);
        $criteria->compare('product_id', $this->product_id, true);
        $criteria->compare('supplier_id', $this->supplier_id, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('price', $this->price);
        $criteria->compare('quantity', $this->quantity, true);
        $criteria->compare('sales', $this->sales);
        $criteria->compare('features', $this->features, true);
        $criteria->compare('size', $this->size, true);
        $criteria->compare('color', $this->color, true);
        $criteria->compare('material', $this->material, true);
        $criteria->compare('is_onstock', $this->is_onstock);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function listItem()
    {
        $criteria = new CDbCriteria;
        $criteria->with = 'product';
        $criteria->with = 'supplier';
        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                    'sort' => array(
                        'defaultOrder' => 'item_id desc'
                    ),
                    'pagination' => array(
                        'pageSize' => 5
                    ),
                ));
    }

    public function addQuantityAndPrice($quantity, $price = '')
    {
        $attributes = array();
        $attributes['quantity'] = $this->quantity + $quantity;
        if( !empty($price) )
        {
            $attributes['price'] =  $price;
        }
        if ($attributes['quantity'] > 0) {
            $attributes['is_onstock'] = 1;
        } else {
            $attributes['is_onstock'] = 0;
        }
        $intentory = new Inventory();
        $intentory->setAttributes(array(
            'item_id'   => $this->item_id,
            'user_id'   => Yii::app()->user->id,
            'quantity'  => $quantity,
            'price'     => $price,
            'date'      => time()
        ));
        if( $intentory->save() )
        {
            $this->updateByPk($this->item_id, $attributes);
        }
    }
}
